<template>
  <div class="loginBox"
       @keyup.enter="loginEvent">
    <el-form :model="loginForm"
             :rules="loginRules"
             ref="loginRef">
      <el-form-item prop="username">
        <el-input prefix-icon="el-icon-user"
                  v-model="loginForm.username"
                  placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input prefix-icon="el-icon-lock"
                  v-model="loginForm.password"
                  placeholder="密码"
                  show-password></el-input>
      </el-form-item>
      <el-form-item class="loginButtonWrapper">
        <el-button class="loginButton"
                   type="primary"
                   @click="loginEvent">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 将websocket进行注册
import Vue from 'vue'
import VueNativeSock from 'vue-native-websocket'
export default {
  data () {
    return {
      // 登录表单数据
      loginForm: {
        username: '1216710774',
        password: '123456'
      },

      // 登录规则
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 4,
            max: 15,
            message: '长度在 4 到 15 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 4,
            max: 15,
            message: '长度在 4 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },

  methods: {
    loginEvent () {
      this.$refs.loginRef.validate(async (valid) => {
        if (valid) {
          this.$http.get('user', { params: this.loginForm }).then((response) => {
            const res = response.data
            if (res.code !== 200) return this.$message.error(res.msg)
            this.$message.success(res.msg)
            // 将账户信息存入session中
            window.sessionStorage.setItem('selfInfo', this.loginForm.username)

            // 注册socket
            const socketUrl = 'ws://8.131.93.118:8082' + '/WebSocket/' + this.loginForm.username
            Vue.use(VueNativeSock, socketUrl, {
              reconnection: true, // 自动重新连接 (false)
              reconnectionAttempts: Infinity, // 重新连接尝试次数 (Infinity),
              reconnectionDelay: 10000, // 重新连接时间间隔
              format: 'json' // json格式传输消息
            })

            // 将自己的状态修改为在线
            this.setOnline()

            // 跳转
            this.$router.push('/mainbox')
          })
        } else return false
      })
    },
    // 将自己的状态修改为在线
    setOnline () {
      this.$http.get('online', { params: { loginID: this.loginForm.username } }).then((response) => {
        const res = response.data
        if (res.code !== 200) return this.$message.error(res.msg)
        this.$message.success(res.msg)
      })
    }
  }

}
</script>

<style lang="less" scoped>
.loginBox {
  padding: 50px;
}

.loginButton {
  float: right;
}
</style>
